package com.layui.ad.controller;

import com.layui.ad.exception.ServiceException;
import com.layui.ad.utils.JsonResult;
import org.apache.shiro.authz.UnauthorizedException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class GlobalExceptionHandler {
    private static final Logger LOGGER = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    /**
     * 处理所有不可知的异常
     * @param e
     * @return
     */
    @ExceptionHandler({Exception.class})
    @ResponseBody
    JsonResult handleException(Exception e){
        LOGGER.error(e.getMessage(), e);
//        AppResponse response = new AppResponse();
        return JsonResult.fail("操作失败！");
    }

    /**
     * 处理所有业务异常
     * @param e
     * @return
     */
    @ExceptionHandler(ServiceException.class)
    @ResponseBody
    JsonResult handleBusinessException(ServiceException e){
        LOGGER.error(e.getMessage(), e);
        return JsonResult.fail(e.getMessage());
    }



    @ExceptionHandler(UnauthorizedException.class)
    @ResponseBody
    public JsonResult defaultExceptionHandler(Exception e){
        return JsonResult.fail("对不起，你没有访问权限！");
    }

//    @ExceptionHandler(value = Exception.class)
//    public ModelAndView defaultErrorHandler(HttpServletRequest req, Exception e) throws Exception {
//        ModelAndView mav = new ModelAndView();
//        mav.addObject("exception", e);
//        mav.addObject("url", req.getRequestURL());
//        mav.setViewName("admin/error/404");
//        return mav;
//    }
}
